/*
 * Copyright 2008 Mark Davidson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.doofer

import com.documentum.com.*
import com.documentum.fc.client.*
import com.documentum.fc.common.*

class ContentUtil {

    /**
     * Constructor
     */
    ContentUtil() {}

    /**
     * Gets content as a ByteArrayInputStream for an IDfSysObject.
     * The named parameters are:
     *
     * <ul>
     * <li>object - the IDfSysObject object</li>
     * <li>format - the file format (optional)</li>
     * <li>pageNum - the page number (optional)</li>
     * </ul>
     *
     * @param params The named parameters. See description for more info.
     * @throws DfException
     */
    def getContent(Map params) {
        def object = params.object
        def format = params.format
        def pageNum = params.pageNum
        def bais = null

        if(!format) {
            bais = object?.getContent()
        } else {
            if(!pageNum) pageNum = 0

            bais = object?.getContentEx(format, pageNum)
        }

        bais
    }

    /**
     * Exports content to a File. Please note that the method does not
     * check the format type! The named parameters are:
     *
     * <ul>
     * <li>object - the IDfSysObject object<li>
     * <li>format - the file format (optional)<li>
     * <li>pageNum - the page number (optional)<li>
     * </ul>
     *
     * @param params The named parameters. See description for more info.
     * @throws DfException
     */
    def exportContent(Map params) {
        def file = null
        def target = params.target
        def bais = getContent(params)

        if(bais) {
            file = new File(target)
            def output = new FileOutputStream(file)
            try {
                output << bais
            } finally {
                output.close()
            }
        }
        file
    }

    /**
     * Gets content as a String for an IDfSysOtbject. Please note that the
     * method does not check the format type!
     * The named parameters are:
     *
     * <ul>
     * <li>object - the IDfSysObject object<li>
     * <li>format - the file format (optional)<li>
     * <li>pageNum - the page number (optional)<li>
     * </ul>
     *
     * @param params The named parameters. See description for more info.
     * @throws DfException
     */
    def getText(Map params) {
        def bais = getContent(params)
        bais ? new DfClientX().ByteArrayInputStreamToString(bais) : null
    }

    /**
     * Sets the content for a persistent object. The content source can
     * be a ByteArrayInputStream or File. The named parameters are:
     *
     * <ul>
     * <li>object - the IDfSysObject</li>
     * <li>format - the file format (optional)</li>
     * <li>pageNum - the page number (optional)</li>
     * <li>content - the ByteArrayInputStream instance (optional)</li>
     * <li>filePath - the path to the file instance (optional)</li>
     * <li><otherFile - resource fork for Mac docs (optional)/li>
     * </ul>
     *
     * @param params The named parameters. See description for further info.
     */
    def setContent(Map params) {
        def object = params.object
        def content = params.content
        def filePath = params.filePath
        def format = params.format
        def pageNum = params.pageNum ?: 0
        def otherFile = params.otherFile

        if(object) {
            if(content) {
                if(format) object.setContentEx(content, format, pageNum)
                else object.setContent(content)
            } else if(filePath) {
                def file = new File(filePath)
                if(file.exists() && file.isFile()) {
                    if(format) object.setFileEx(filePath, format, pageNum, otherFile)
                    else object.setFile(filePath)
                }
            }
        }
    }
}